//+------------------------------------------------------------------+
//|                                            Pulse MACD_modify.mq4 |
//|                                Copyright  2005, David W. Thomas |
//|                                          2009, modified by Rokai |
//|                                          e-mail: rokai@yahoo.com |
//+------------------------------------------------------------------+
#property copyright "Copyright  2005, David W. Thomas (2009, modified by Rokai)"
#property link      "e-mail: rokai@yahoo.com"

//----  
#property indicator_separate_window
#property indicator_buffers 5

//----  
#property indicator_color1 DimGray
#property indicator_color2 DimGray
#property indicator_color3 DarkSlateGray
#property indicator_color4 SteelBlue
#property indicator_color5 Green

//----  
extern int       FastMAPeriod=9;
extern int       SlowMAPeriod=26;
extern int       SignalMAPeriod=3;
extern bool      GSignal=false;
extern string    SoundFileName = "";
double           NormAccuracy = 0.0000;

//---- 
double MACD[];
double MACDSignal[];
double Histogram[];
double Cross_Up[];
double Cross_Down[];

//---- 
double alpha = 0;
double alpha_1 = 0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   //----  MACD
   SetIndexStyle(0,DRAW_LINE,EMPTY,1);
   SetIndexDrawBegin(0,SlowMAPeriod);
   SetIndexBuffer(0,MACD);
   
   //----  
   SetIndexStyle(1,DRAW_LINE,EMPTY,1);
   SetIndexDrawBegin(1,SlowMAPeriod+SignalMAPeriod);
   SetIndexBuffer(1,MACDSignal);
     
   //----  UP
   SetIndexStyle(3,DRAW_ARROW,EMPTY,1);
   SetIndexArrow(3,167);
   SetIndexBuffer(3,Cross_Up);
   
   //----  DOWN
   SetIndexStyle(4,DRAW_ARROW,EMPTY,1);
   SetIndexArrow(4,167);
   SetIndexBuffer(4,Cross_Down);
   
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
   
   //----   
   IndicatorShortName("Pulse MACD ("+FastMAPeriod+","+SlowMAPeriod+","+SignalMAPeriod+")");
   SetIndexLabel(0,"MACD");
   SetIndexLabel(1,"MACD Signal");
   SetIndexLabel(2,"Histogram");
   SetIndexLabel(3,"Buy");
   SetIndexLabel(4,"Sell");
   
   //----
	alpha = 2.0 / (SignalMAPeriod + 1.0);
	alpha_1 = 1.0 - alpha;
	
   //----
   return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   //---- 
   
   //----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{  
   static bool b_Buy  = False;
   static bool b_Sell = False;
    
          bool   bCondition_Up;
          bool   bCondition_Down;
          double Range;
          double Avg_Range;
          int    iLimit;
          int    i;
          int    counter;
          int    counted_bars = IndicatorCounted();
   
   //---- check for possible errors
   if (counted_bars<0) 
     return(-1);
     
   //---- last counted bar will be recounted
   if (counted_bars>0) counted_bars--;
   
   iLimit = Bars - counted_bars;

   for(i=iLimit; i>=0; i--)
   {
     MACD[i] = iMA(NULL,0,FastMAPeriod,0,MODE_EMA,PRICE_CLOSE,i) - iMA(NULL,0,SlowMAPeriod,0,MODE_EMA,PRICE_CLOSE,i);
     MACDSignal[i] = alpha*MACD[i] + alpha_1*MACDSignal[i+1];
     Histogram[i] = MACD[i] - MACDSignal[i];
   }
   
   
   
   for ( i = 1; i <= iLimit; i++ ) 
   {
     Avg_Range = 0;
       for ( counter = i; counter <= i + 9; counter++ ) 
       {
         Avg_Range += MathAbs( High[ counter ] - Low[ counter ] );
       }
     Range = Avg_Range/10;
   
     bCondition_Up   = ( MACD[i] >= MACDSignal[i] ) &&
                      ( MACD[i+1] <= MACDSignal[i+1] ) &&
                      ( MACD[i-1] >= MACDSignal[i-1] )&& //  UP
                      ( NormalizeDouble(MACD[i-1]- MACD[i+1],4)>=NormAccuracy);
                      
     bCondition_Down = ( MACD[i] <= MACDSignal[i] ) &&
                      ( MACD[i+1] >= MACDSignal[i+1] ) &&
                      ( MACD[i-1] <= MACDSignal[i-1] ) && //  DOWN
                      ( NormalizeDouble(MACD[i+1]- MACD[i-1],4)>=NormAccuracy);
   
    
    
    Cross_Up[i] = EMPTY_VALUE;
    Cross_Down[i] = EMPTY_VALUE;
     if ( bCondition_Up )
     Cross_Up[i] = MACDSignal[i];
     else 
     if ( bCondition_Down )
     Cross_Down[i] = MACDSignal[i];
    
     if ( !b_Buy && bCondition_Up ) 
     {
       // 
       b_Buy  = True;  //   BUY
       b_Sell = False; //   SELL
      
      
       if ( i < 2 && GSignal == True ) 
       {
         Alert (Symbol()," ",Period(),"M  CROSS BUY "); //  
           if ( SoundFileName != "" )
           PlaySound( SoundFileName );
       } 
      
     }
     else 
     if ( !b_Sell && bCondition_Down ) 
     {
       // 
       b_Sell = True;  //   SELL
       b_Buy  = False; //   BUY
      
 
       if ( i < 2 && GSignal == True) 
       {
         Cross_Down[i] = MACDSignal[i];
         Alert (Symbol()," ",Period(),"M   CROSS SELL "); //  
           if ( SoundFileName != "" )
           PlaySound( SoundFileName );
       } 
     } 
   }  
   //----
   return(0);
}
//+-----------------------------------------------------------------